﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Data.SqlClient;
using CNCSoft.EData;

using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraEditors.Controls;

using Common;

namespace CNCSoft.ErpApps.TMS.Counsellor
{
    public partial class frmInGiayTiepNhan : Form
    {
        public frmInGiayTiepNhan()
        {
            InitializeComponent();
        }

        private SqlDataAdapter dataAdapter = new SqlDataAdapter();
        DataSet ds = new DataSet();

        private void frmInGiayTiepNhan_Load(object sender, EventArgs e)
        {
            GetDSXetTuyen(false);
            bandedGridView1.IndicatorWidth = 35;  //Kich thuoc tieu de hang

            //Add item cho cbo KhaoSatTuCach
            repositoryItemComboBox_KhaoSatTuCach.Items.Add("ĐẢM BẢO");
            repositoryItemComboBox_KhaoSatTuCach.Items.Add("KHÔNG ĐẢM BẢO");
        }


        private void GetDSXetTuyen(bool bChiXemChuaTN)
        {
            try
            {

                if (bChiXemChuaTN)
                    dataAdapter = new SqlDataAdapter("SELECT MaTuyenSinh, HoLot + ' ' + Ten as HoTen, case gioitinh when '1' then N'Nam' else N'Nữ' end as GioiTinh, MaKhoiDangKy, TenCheDo, MaCoSo, " +
                                                 "NgayNopHoSo, NgayDuyet, NguoiDuyet, MaKetQuaXetTuyen, CoSoTiepNhan, LyDoXetDuyet, " +
                                                 "MaTrangThaiXetTuyen, HanDangKy, SoDienThoai, NhanVienTiepNhan, GhiChu, NhanVienNhanHoSo, KhaoSatTuCach " +
                                                 "FROM TUYENSINH M " +
                                                 "LEFT JOIN DM_CHEDO ON M.MaCheDo = DM_CHEDO.MaCheDo " +
                                                 "WHERE MaKetQuaXetTuyen = N'ĐẠT' " +
                                                 "AND MaTrangThaiXetTuyen = '01' "
                                                 , GlobalVariable.WSInfo.ConnectionString);
                else
                    dataAdapter = new SqlDataAdapter("SELECT MaTuyenSinh, HoLot + ' ' + Ten as HoTen, case gioitinh when '1' then N'Nam' else N'Nữ' end as GioiTinh, MaKhoiDangKy, TenCheDo, MaCoSo, " +
                                                 "NgayNopHoSo, NgayDuyet, NguoiDuyet, MaKetQuaXetTuyen, CoSoTiepNhan, LyDoXetDuyet, " +
                                                 "MaTrangThaiXetTuyen, HanDangKy, SoDienThoai, NhanVienTiepNhan, GhiChu, NhanVienNhanHoSo, KhaoSatTuCach " +
                                                 "FROM TUYENSINH M " +
                                                 "LEFT JOIN DM_CHEDO ON M.MaCheDo = DM_CHEDO.MaCheDo " +
                                                 "WHERE MaKetQuaXetTuyen = N'ĐẠT' OR  MaKetQuaXetTuyen = N'THI ĐẠT' "
                                                 , GlobalVariable.WSInfo.ConnectionString);
                
                if (ds.Tables["TUYENSINH"] != null)
                    ds.Tables["TUYENSINH"].Clear();
                dataAdapter.Fill(ds, "TUYENSINH");

                grid_DSXetTuyen.DataSource = ds;
                grid_DSXetTuyen.DataMember = "TUYENSINH";

                
                if (ds.Tables["DM_KETQUAXETTUYEN"] != null)
                    ds.Tables["DM_KETQUAXETTUYEN"].Clear();
                dataAdapter = new SqlDataAdapter("SELECT * FROM DM_KETQUAXETTUYEN", GlobalVariable.WSInfo.ConnectionString);
                dataAdapter.Fill(ds, "DM_KETQUAXETTUYEN");
                repositoryItemLookUpEdit_KetQuaXetTuyen.DataSource = ds.Tables["DM_KETQUAXETTUYEN"];

                if (ds.Tables["DM_TRANGTHAIXETTUYEN"] != null)
                    ds.Tables["DM_TRANGTHAIXETTUYEN"].Clear();
                dataAdapter = new SqlDataAdapter("SELECT * FROM DM_TRANGTHAIXETTUYEN", GlobalVariable.WSInfo.ConnectionString);
                dataAdapter.Fill(ds, "DM_TRANGTHAIXETTUYEN");
                repositoryItemLookUpEdit_TrangThaiXetTuyen.DataSource = ds.Tables["DM_TRANGTHAIXETTUYEN"];

                bandedGridView1.BestFitColumns();

            }
            catch (SqlException ex)
            {
                MessageBox.Show("Error: \n" + ex.Message);
            }

        }

        private void cmdInGBTN_Click(object sender, EventArgs e)
        {
            if (!((ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["KhaoSatTuCach"].ToString() == "ĐẢM BẢO" &&
                ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["HanDangKy"] != DBNull.Value)))
            {
                MessageBox.Show("Thí sinh này không đảm bảo tư cách hoặc đã đạt nhưng chưa có hạn đăng ký\nKhông in giấy tiếp nhận", "In giấy TN", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            //In giay TN
            string strRptGBTN = "";
            try
            {
                switch (ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaKhoiDangKy"].ToString())
                {
                    case "01":
                    case "02":
                    case "03":
                    case "04":
                    case "05":
                        strRptGBTN = "sp_Print_GiayBaoTiepNhan_TieuHoc.rpt";
                        break;
                    case "06":
                    case "07":
                    case "08":
                    case "09":
                        strRptGBTN = "sp_Print_GiayBaoTiepNhan_THCS.rpt";
                        break;
                    case "10":
                    case "11":
                    case "12":
                        strRptGBTN = "sp_Print_GiayBaoTiepNhan.rpt";
                        break;
                }

                cmdInGBTN.Enabled = false;

                BindingManagerBase bmb = this.BindingContext[ds, "TUYENSINH"];

                //Load report
                ReportDocument rpt = new ReportDocument();
                rpt.Load(Application.StartupPath + "\\Report\\" + strRptGBTN);

                //Set connection
                PublicFunction.SetDBLogonForReport(rpt);

                //Set parameter
                //PublicFunction.SetParameter(rpt, "@pMaTuyenSinh", ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaTuyenSinh"].ToString());
                PublicFunction.SetParameter(rpt, "@pMaTuyenSinh", ds.Tables["TUYENSINH"].Rows[bmb.Position]["MaTuyenSinh"].ToString());

                PublicFunction.SetParameter(rpt, "pMoRongDong", false);

                //View report
                frmPrintPreview frmPP = new frmPrintPreview(rpt);
                frmPP.ShowDialog();
                frmPP.Dispose();

                rpt.Dispose();
                cmdInGBTN.Enabled = true;

                //Doi trang thai
                //ds.Tables["TUYENSINH"].Rows[bandedGridView1.FocusedRowHandle]["MaTrangThaiXetTuyen"] = "02";
                ds.Tables["TUYENSINH"].Rows[bmb.Position]["MaTrangThaiXetTuyen"] = "02";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdUpDate_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                string sqlUpdate = "UPDATE TUYENSINH SET KhaoSatTuCach = @pKhaoSatTuCach, HanDangKy = @pHanDangKy, MaTrangThaiXetTuyen = @pMaTrangThaiXetTuyen " +
                                   "WHERE MaTuyenSinh = @pMaTuyenSinh";

                SqlParameter[] pUpdate = new SqlParameter[4];

                pUpdate[0] = new SqlParameter("@pKhaoSatTuCach", SqlDbType.NVarChar, 20, "KhaoSatTuCach");
                pUpdate[1] = new SqlParameter("@pHanDangKy", SqlDbType.Date, 10, "HanDangKy");
                pUpdate[2] = new SqlParameter("@pMaTrangThaiXetTuyen", SqlDbType.VarChar, 2, "MaTrangThaiXetTuyen");

                pUpdate[3] = new SqlParameter("@pMaTuyenSinh", SqlDbType.VarChar, 20, "MaTuyenSinh");

                SqlCommand cmdUpdate = new SqlCommand(sqlUpdate, cnn);
                cmdUpdate.Parameters.AddRange(pUpdate);

                dataAdapter.UpdateCommand = cmdUpdate;
                try
                {
                    dataAdapter.Update(ds, "TUYENSINH");
                    ds.AcceptChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bandedGridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle > -1)
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            ds.RejectChanges();
        }

        private void chkChiXemTHChuaTN_CheckedChanged(object sender, EventArgs e)
        {
            GetDSXetTuyen(chkChiXemTHChuaTN.Checked);
        }

       

    }

}
